import { Caller } from "../core/call/Caller";
import { Application } from "../ui/Application";
//class Global
// did
export class Global {
  constructor() {
    this._caller = new Caller();
    if (Global._instance != null) {
      throw new Error("Global 单例");
    }
  }

  public initStage(arg1: any /* flash.display.Stage */): void {
    // Global._stage = arg1;
    // Global._stage.addEventListener(flash.events.Event.RESIZE, this.onReSizeHandler, false, 99999, false);
    // Global._stage.addEventListener(flash.events.Event.ACTIVATE, this.onActivateHandler, false, 99999);
    // Global._stage.addEventListener(flash.events.Event.DEACTIVATE, this.onDeactivateHandler, false, 99999);
  }

  /* internal  */ onRenderHandler(arg1: any /* flash.events.Event */): void {
    this._isCallLater = false;
    this._caller.call(Global.CallerType);
    this._caller.removeCallByType(Global.CallerType);
  }

  /* internal  */ onActivateHandler(
    arg1: Object /* flash.events.Event */
  ): void {
    // Global._stage.focus = stage;
    Global.isActivate = true;
  }

  /* internal  */ onDeactivateHandler(
    arg1: Object /* flash.events.Event */
  ): void {
    Global.isActivate = false;
    return;
  }

  /* internal  */ onReSizeHandler(arg1: Object /* flash.events.Event */): void {
    // if (arg1.target is flash.display.Stage == false)
    // {
    //     arg1.stopImmediatePropagation();
    // }
    return;
  }

  public callLater(arg1: Function): void {
    this._caller.addCall(Global.CallerType, arg1);
    if (this._isCallLater == false) {
      // Global._stage.invalidate();
      // Global._stage.addEventListener(flash.events.Event.RENDER, this.onRenderHandler);
      this._isCallLater = true;
    }
    return;
  }

  public static get instance(): Global {
    if (Global._instance == null) {
      Global._instance = new Global();
    }
    return Global._instance;
  }

  public static get stage(): Object /* flash.display.Stage */ {
    return Global._stage;
  }

  public static gc(): void {
    try {
      // new Object /* flash.net.LocalConnection */().connect("foo");
      // new Object /* flash.net.LocalConnection */().connect("foo");
    } catch (e) {}
    return;
  }

  private static /* const */ CallerType: string = "render";

  private /* var */ _caller: Caller;

  private /* var */ _isCallLater: Boolean = false;

  private static /* var */ _stage: any /* flash.display.Stage */;

  public static /* var */ isDebugModle: Boolean = false;

  public static /* var */ isActivate: Boolean = true;

  private static /* var */ _instance: Global;

  public static /* var */ application: Application;
}
